﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FJASPX.Framwork.Dao;
using FJASPX.Domain;
using FJASPX.Framwork.Query;
using NHibernate.Criterion;
using NHibernate;

namespace FJASPX.Service.Implement
{
    /// <summary>
    /// 企业服务
    /// </summary>
    public class EnterpriseService : NHEntityDao<Enterprise>, IEnterpriseService
    {

        public Page PagedQuery(DetachedCriteria detachedCriteria, int startIndex, int pageSize, List<Order> orders)
        {
            CriteriaQueryTemplate qt = new DomainCriteriaQueryTemplate(SessionFactory);
            return qt.PagedQuery(detachedCriteria, startIndex, pageSize, orders);
        }

        public int GetRowCount(DetachedCriteria detachedCriteria)
        {
            CriteriaQueryTemplate qt = new DomainCriteriaQueryTemplate(SessionFactory);
            return qt.RowCountQuery(detachedCriteria);
        }

        public IList<Enterprise> PagedQuery(ICriterion expression, List<Order> order, int pageIndex, int pageSize, out long recordCount)
        {
            return this.Query<Enterprise>(expression, order, pageIndex, pageSize, out recordCount);
        }
        public IList<Enterprise> PagedQuery(List<ICriterion> expressions, List<Order> order, int pageIndex, int pageSize, out long recordCount)
        {
            return this.Query<Enterprise>(expressions, order, pageIndex, pageSize, out recordCount);
        }

        public Enterprise GetByUser(User user)
        {
            string hql = "from Enterprise where User.Id like :userId";
            IQuery query = this.Session.CreateQuery(hql);
            query.SetParameter("userId", user.Id);
            query.SetMaxResults(1);
            System.Collections.IList list = query.List();
            if (list.Count > 0)
            {
                return (Enterprise)list[0];
            }
            return null;
        }
    }
}
